package cn.fafu.fafu_record.controller;


import cn.fafu.fafu_record.annotation.PassToken;
import cn.fafu.fafu_record.annotation.VerifyToken;
import cn.fafu.fafu_record.entity.User;
import cn.fafu.fafu_record.result.Result;
import cn.fafu.fafu_record.utils.JwtUtil;
import cn.fafu.fafu_record.utils.ResultUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
public class UserController {

    @GetMapping("/getToken")
    @PassToken
    @ResponseBody
    public Result getToken(@RequestParam("username") String username){
        User user = new User(username);
        String token = JwtUtil.getToken(user);
        Map<String,String> map = new HashMap<>();
        map.put("token",token);
        return ResultUtil.success(map);

    }

    @PostMapping("/verifyToken")
    @VerifyToken
    @ResponseBody
    public Result verifyToken(@RequestParam("token") String token){

        String username = JwtUtil.decodeToken(token);
        Map<String,String> map = new HashMap<>();
        map.put("username",username);
        return ResultUtil.success(map);
    }

}
